package com.tianlu.store.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import java.time.Duration;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

@Component
public class RedisTemplateUtils {
    private static final Logger log = LoggerFactory.getLogger(RedisTemplateUtils.class);
    private static RedisTemplate<String, Object> redisTemplate;

    public RedisTemplateUtils() {
    }

    @Resource
    public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
        RedisTemplateUtils.redisTemplate = redisTemplate;
    }

    public static void setKey(String key, String value) {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        ops.set(key, value);
    }

    public static Object getHashValue(String key) {
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
        Map<Object, Object> entries = opsForHash.entries(key);
        return entries;
    }

    public static Object getHashValueByHashKey(String key, Object hashKey) {
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
        Object o = opsForHash.get(key, hashKey);
        return o;
    }

    public static void setHashValue(String key, Object hashKey, Object object) {
        HashOperations<String, Object, Object> opsForHash = redisTemplate.opsForHash();
        opsForHash.put(key, hashKey, object);
    }

    public static String getValue(String key) {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        return Objects.requireNonNull(ops.get(key)).toString();
    }

    public static boolean exist(String key) {
        return Boolean.TRUE.equals(redisTemplate.hasKey(key));
    }

    public static RedisTemplate<String, Object> getRedisTemplate() {
        return redisTemplate;
    }

    public static boolean expire(String key, long time) {
        try {
            if (time > 0L) {
                redisTemplate.expire(key, time, TimeUnit.SECONDS);
            }

            return true;
        } catch (Exception var4) {
            log.error(var4.getMessage(), var4);
            return false;
        }
    }

    public static long getExpire(String key) {
        return redisTemplate.getExpire(key, TimeUnit.SECONDS);
    }

    public static boolean hasKey(String key) {
        try {
            return Boolean.TRUE.equals(redisTemplate.hasKey(key));
        } catch (Exception var2) {
            log.error(var2.getMessage(), var2);
            return false;
        }
    }

    public static void del(String... key) {
        if (key != null && key.length > 0) {
            if (key.length == 1) {
                redisTemplate.delete(key[0]);
            } else {
                redisTemplate.delete(CollectionUtil.newArrayList(key));
            }
        }

    }

    public static void delByHashKey(String key, String hashKey) {
        if (StrUtil.isNotBlank(key) && StrUtil.isNotBlank(hashKey)) {
            redisTemplate.opsForHash().delete(key, new Object[]{hashKey});
        }

    }

    public static boolean setIfAbsent(String key, String val, int seconds) {
        Duration duration = Duration.ofSeconds((long)seconds);
        return Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(key, val, duration));
    }

    public static Object get(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    public static boolean set(String key, Object value) {
        try {
            redisTemplate.opsForValue().set(key, value);
            return true;
        } catch (Exception var3) {
            log.error(var3.getMessage(), var3);
            return false;
        }
    }

    public static boolean set(String key, Object value, long time) {
        try {
            if (time > 0L) {
                redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
            } else {
                set(key, value);
            }

            return true;
        } catch (Exception var5) {
            log.error(var5.getMessage(), var5);
            return false;
        }
    }

    public static long incr(String key, long delta) {
        if (delta < 0L) {
            throw new RuntimeException("递增因子必须大于0");
        } else {
            return redisTemplate.opsForValue().increment(key, delta);
        }
    }

    public static long decr(String key, long delta) {
        if (delta < 0L) {
            throw new RuntimeException("递减因子必须大于0");
        } else {
            return redisTemplate.opsForValue().increment(key, -delta);
        }
    }
}

